package org.dtrd.modules.organization.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.organization.entity.bean.DoctorOrganizationDetail;
import org.dtrd.modules.organization.entity.bean.DoctorOrganizationListInfo;
import org.dtrd.modules.organization.entity.request.DoctorOrganizationListRequest;
import org.dtrd.modules.organization.entity.request.DoctorOrganizationPageRequest;
import org.dtrd.modules.organization.service.IDtrdRlDoctorOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 * 机构和医生关系表 前端控制器
 * </p>
 * @author QR
 * @since 2021-12-14
 * @deprecated 肖 2022-06-23 16:11:00
 */
@Deprecated
@Api(tags = "机构信息",hidden = true)
@RestController
@RequestMapping("/organization/dtrd-rl-doctor-organization")
public class DoctorOrgRelationController extends BaseController {

    @Autowired
    private IDtrdRlDoctorOrganizationService doctorOrganizationService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation(value = "查询机构和医生关系表详情", response = DoctorOrganizationDetail.class)
    @GetMapping("get")
    public Result<?> getDoctorOrganization(@RequestParam Integer id){
        DoctorOrganizationDetail info = doctorOrganizationService.getDoctorOrganization(id);
        String logContent = String.format("查询机构和医生关系表详情，查询id：%d", id);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(info);
    }

    @ApiOperation(value = "查询机构和医生关系表列表", response = DoctorOrganizationListInfo.class)
    @PostMapping("list")
    @AutoLog(value="查询机构和医生关系表列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getDoctorOrganizationList(@RequestBody DoctorOrganizationListRequest request){
        List<DoctorOrganizationListInfo> list = doctorOrganizationService.getDoctorOrganizationList(request);
        String logContent = String.format("查询机构和医生关系表列表, 查询条件: %s", request.toString());
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(list);
    }

    @ApiOperation(value = "分页查询机构和医生关系表列表", response = DoctorOrganizationListInfo.class)
    @PostMapping("page")
    public Result<?> getDoctorOrganizationList(@RequestBody DoctorOrganizationPageRequest request){
        handlePageRequest(request);
        IPage<DoctorOrganizationListInfo> page = doctorOrganizationService.pageDoctorOrganization(request);
        String logContent = String.format("分页查询机构和医生关系表列表, 查询条件: %s", request.toString());
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(page);
    }

    @ApiOperation("新增或修改机构和医生关系表")
    @PostMapping("edit")
    public Result<?> saveOrUpdateDoctorOrganization(@RequestBody DoctorOrganizationDetail dto){
        boolean result = doctorOrganizationService.saveOrUpdateDoctorOrganization(dto);
        String logContent = String.format("新增或修改机构和医生关系表");
        baseCommonService.addLog(logContent, OPERATE_TYPE_SAVE_OR_UPDATE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除机构和医生关系表")
    @DeleteMapping("remove")
    @AutoLog(value="删除机构和医生关系表", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeDoctorOrganization(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = doctorOrganizationService.removeDoctorOrganization(id);
        String logContent = String.format("删除机构和医生关系表, 删除id: %d", id);
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
